package com.then.architecture.databinding;

import android.content.ClipData;
import android.databinding.BindingAdapter;
import android.support.v7.widget.RecyclerView;

import com.then.architecture.view.MyStickyHeaderAdapter;
import com.then.architecture.view.StickyListGroup;
import com.timehop.stickyheadersrecyclerview.StickyRecyclerHeadersDecoration;

import java.sql.Time;
import java.util.ArrayList;
import java.util.List;

import ca.barrenechea.widget.recyclerview.decoration.DividerDecoration;
import ca.barrenechea.widget.recyclerview.decoration.StickyHeaderDecoration;
import me.tatarka.bindingcollectionadapter2.BindingRecyclerViewAdapter;
import me.tatarka.bindingcollectionadapter2.ItemBinding;
import timber.log.Timber;

/**
 * Created by 42524 on 2018/2/6.
 */

public class BindingRecyclerViewAdapters {

    @SuppressWarnings("unchecked")
    @BindingAdapter(value = {"itemBinding", "items", "adapter", "itemIds", "viewHolder", "stickyListGroup"}, requireAll = false)
    public static <T> void setAdapter(RecyclerView recyclerView, ItemBinding<T> itemBinding, List<T> items, MyStickyHeaderAdapter<T> adapter, BindingRecyclerViewAdapter.ItemIds<? super T> itemIds, BindingRecyclerViewAdapter.ViewHolderFactory viewHolderFactory, StickyListGroup stickyListGroup) {
        if (itemBinding == null) {
            throw new IllegalArgumentException("itemBinding must not be null");
        }
        MyStickyHeaderAdapter oldAdapter = (MyStickyHeaderAdapter) recyclerView.getAdapter();
        if (adapter == null) {
            if (oldAdapter == null) {
                adapter = new MyStickyHeaderAdapter<>();
            } else {
                adapter = oldAdapter;
            }
        }
        adapter.setItemBinding(itemBinding);
        adapter.setItems(items);
        adapter.setItemIds(itemIds);
        adapter.setViewHolderFactory(viewHolderFactory);
        if (oldAdapter != adapter) {
            recyclerView.setAdapter(adapter);
            StickyRecyclerHeadersDecoration decor = new StickyRecyclerHeadersDecoration(adapter);
            adapter.setStickyListGroup(stickyListGroup);
            recyclerView.addItemDecoration(decor);
        }
    }
}
